package com.pacage;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class HelloServlet implements Servlet {
    public HelloServlet() {
        System.out.println("1.构造方法");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

        System.out.println("2 初始方法");
        //1.乐意获取Servlet程序的别名servlet-name的值
        System.out.println("HelloServlet程序的别名是;"+servletConfig.getServletName());
        //2.获取初始化参数init.param
        System.out.println("初始化username的值是;"+servletConfig.getInitParameter("username"));
        System.out.println("初始化url的值是;"+servletConfig.getInitParameter("url"));
        //3.获取Servlet对象
        System.out.println(servletConfig.getServletContext());
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * service方法用来处理请求和响应
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;
        String method=httpServletRequest.getMethod();
        if("GET".equals(method)){
            doGet();
        }else if("POST".equals(method)){
            doPost();
        }
//        System.out.println(method);
//        System.out.println("3.  service被访问");
    }

    /**
     * 做get请求的操作
     */
    public  void doGet(){
        System.out.println("get请求");
    }
    /**
     * 做post请求的操作
     */
    public  void doPost(){
        System.out.println("post请求");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("4. destory方法");

    }
}
